<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Quiz/classes/PhpFox_ComponentAddQuiz.class.php, $Author:  $ */



/** List of quizzes
 * 
 * Parameters:
 * 1. oUser (required) - quiz author object
 * 
 *   
 * @version $Id: PhpFox_ComponentAddQuiz.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 * @package module.quiz
 */
class PhpFox_Mod_Quiz_ComponentAddQuiz extends BaseComponent
{
    
    /** Processing
     */
    function process()
    {
        $oSrvQuiz = &$this->_oModule->getService('Quiz');
        /* @var $oSrvQuiz PhpFox_Mod_Quiz_ServiceQuiz */
        
        $oReq = &App::getRequest();
        /* @var $oReq Request */
        
        App::loadClass('Text');
        $oTxt = &new Text();
        
        $oSrvSec = &App::getModuleService('Account', 'Security');
        /* @var $oSrvSec PhpFox_Mod_Account_ServiceSecurity */
        
        $oSrvSec->checkPerm('quiz', 'full');
        
        //create empty quiz data
        $aQuizData = array();
        for ($i = 1; $i<=App::getParam('Mod_Quiz.max_questions'); $i++)
        {
            $aQuizData['questions'][$i] = '';
        }
        
        $oUser = $this->getParam('oUser', null);
        assert($oUser != null);
        
        $bIsShowResults = false;
        
        switch($oReq->getAction())
        {
            case 'quiz_add':
                $aQuizData['title'] = $oReq->get('title');
                $aQuizData['questions'] = $oReq->getArray('aQuestion');
                for ($i = 1; $i<=App::getParam('Mod_Quiz.max_questions'); $i++)
                {
                    $aQuizData['answers'][$i] = $oReq->getArray('aAnswer'.$i);
                }
                if ($this->_validateQuizData($aQuizData))
                {
                    $bIsShowResults = true;
                    //performs add quiz
                    if ($oSrvQuiz->createQuiz($aQuizData['title'], $aQuizData['questions'],
                                              $aQuizData['answers'], $oUser))
                    {
                        $bIsShowResults = true;
                    }
                }
                break;
        }
        

        $oTpl = &$this->_oModule->getTemplate();
        $oTpl->assign(array(
            'aQuizData'      => $aQuizData,
            'aErrors'        => $this->getErrors(),
            'bIsShowResults' => $bIsShowResults,
            'aUser'          => $oUser->aData,
        ));
        return $oTpl->fetch('AddQuiz.html');
    }

    /** Validates quiz data
     * 
     * @param array $aQuizData quiz data array
     * @return bool true - data ok, false - data conains errors
     */
    function _validateQuizData($aQuizData)
    {
        //check title
        if (!$aQuizData['title'])
        {
            $this->_addError('Mod_Quiz.error.no_title');
        }
        
        //check at least one questions
        $aQuestions = array();
        for ($i = 1; $i<=App::getParam('Mod_Quiz.max_questions'); $i++)
        {
             if ($aQuizData['questions'][$i])
             {
                 $aQuestions[$i] = $aQuizData['questions'][$i];
             }
        }
        if (!$aQuestions)
        {
            $this->_addError('Mod_Quiz.error.no_questions');
        }
        
        foreach($aQuestions as $iQuestion=>$sQuestion)
        {
            //check that question have correct answer
            if (! $aQuizData['answers'][$iQuestion][1])
            {
                $this->_addError('Mod_Quiz.error.no_correct_answer', array($sQuestion));
            }
            
            //check that question have at least one incorrect answer
            if (! ($aQuizData['answers'][$iQuestion][2] ||
                   $aQuizData['answers'][$iQuestion][3] ||
                   $aQuizData['answers'][$iQuestion][4])  )
            {
                $this->_addError('Mod_Quiz.error.no_incorrect_answer', array($sQuestion));
            }
        }
        
        return (! $this->getErrors());
    }
}
?>